<template>
  <div>
    <!--条件筛选-->
    <TopFilter :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle" @resetChange="resetChange"></TopFilter>
    <!--操作 -->
    <button-area :style="{ paddingLeft: '80px' }"></button-area>
    <!-- 列筛选-->
    <FilterTable
      ref="table"
      columnsRef="lhmqueryCarMaintain"
      :columns="columns"
      :fetchapi="fetchApi"
      selectionType="single"
      :params="fetchParams"
      :isExportExcel="true"
      exportFileName="车辆维修信息统计.xlsx"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectDataList"
      :onSyncTableData="tableHandle"
      :isServerColumnsRef="true"
    >
      <template slot="controls">
        <el-button size="small" type="primary" @click="entering">明细</el-button>
        <!-- <el-button size="small" type="primary"  @click="saveHandle">委托书</el-button> -->
        <el-button size="small" type="primary" @click="Portal">Portal查询</el-button>
      </template>
    </FilterTable>
    <BasePrint ref="print" :data="printList" template="entrustOrder" />
    <BaseDialog
      :visible.sync="visible7"
      title="车型"
      :closable="true"
      :destroyOnCloseSaleSingle="true"
      :maskClosable="true"
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <SelectCX @cancelManager7="cancelDialogManager7" @confirmManager="confirmDialogManager7" />
    </BaseDialog>
    <BaseDialog
      :visible.sync="visible8"
      title="主修人"
      :closable="true"
      :destroyOnCloseSaleSingle="true"
      :maskClosable="true"
      :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }"
    >
      <ServiceAdvisor @cancelManager8="cancelDialogManager8" @confirmManager="confirmDialogManager8" />
    </BaseDialog>
    <!-- Portal -->
    <BaseDialog title="Portal" :visible.sync="PortalShow" destroy-on-close :container-style="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <!-- <Modal :parmas="parmas" @close="closePortalHandler" /> -->
    </BaseDialog>
    <!-- 查询明细 -->
    <BaseDialog :visible.sync="labour" title="明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <labourModel :list="selectData" @closeDialog="closeDialog"></labourModel>
    </BaseDialog>
  </div>
</template>

<script>
import dictCode from '@/utils/dictCode';
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import { getUserListSelect } from '@/api/report/repair/repairreport';
import { getCarRepairThing } from '@/api/repair/queryStatistics/queryAccounts';
import { searchRoleByCode, searchRoleByCodes } from '@/api/common/empSelect/index';
import { getAllUser } from '@/api/repair/queryStatistics/queryAccounts';
import labourModel from '@/pages/repair/repairReception/repairBusinessManger/labourModel';
// import Modal from '@/pages/repair/repairReception/customer/Modal/model';
import ServiceAdvisor from './serviceAdvisor';
import Entering from './entering';
import SelectCX from './selectCX';

export default {
  name: 'queryCarM',
  components: {
    Entering,
    SelectCX,
    // Modal,
    labourModel,
    ServiceAdvisor
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', delete: '删除', edit: '编辑', show: '显示' };
    return {
      parmas: null,
      labour: false,
      topFilterList: this.createTopFilterList(),
      fetchParams: { xhrAbort: true },
      visible: false,
      visible1: false,
      visible7: false,
      visible8: false,
      dialogTitle: '',
      printList: {},
      fetchApi: getCarRepairThing,
      columns: this.createTableColumns(),
      handleType: '',
      formData: {},
      tableData: [],
      ids: [],
      modelCode: null,
      selectData: {},
      PortalShow: false,
      actions: { visible: false, type: '', title: '' } // 弹出面板的相关信息
    };
  },
  mounted() {
    // 分页
    // this.BaseTable = this.$refs.table.$refs.pageTable;
    this.initSelect();
    this.queryRepairAndStockMan();
  },
  methods: {
    tableHandle(val) {
      console.log('tableHandle');
    },
    async initSelect() {
      // 服务顾问
      let consultant = { role: dictCode.SERVICE_CONSULTANT }; // 服务顾问
      const res = await searchRoleByCode(consultant);
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').editItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.topFilterList.find(x => x.fieldName === 'SERVICE_ADVISOR').itemList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').filterItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(x => x.dataIndex === 'SERVICE_ADVISOR').dictItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));

      let testDrive = { role: dictCode.TECHNICIAN };
      let res5 = (await searchRoleByCodes(testDrive)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      this.topFilterList.find(item => item.fieldName === 'CHIEF_TECHNICIAN').itemList = res5;

      let chechMan = { role: dictCode.EXAMINATION_CLERK }; // 检验人
      let res6 = (await searchRoleByCode(chechMan)).data.map(x => ({ value: x.userId + '', text: x.employeeName }));
      // this.topFilterList.find(item => item.fieldName === 'CHECKER').itemList = res6;
      this.columns.find(x => x.dataIndex === 'CHECKER').editItems = res6;
    },
    // 查询主修人/领料人翻译字段
    async queryRepairAndStockMan() {
      let res = await getAllUser();
      if (res.resultCode == 200) {
        this.columns.find(x => x.dataIndex === 'CHIEF_TECHNICIAN').dictItems = this.columns.find(x => x.dataIndex === 'CHIEF_TECHNICIAN').filterItems = res.data.map(x => ({
          value: x.userId + '',
          text: x.employeeName
        }));
      }
    },
    createTopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '查询日期类型',
          fieldName: 'DATE_TYPE',
          placeholder: '请选择...',
          initialValue: '1',
          rules: [{ required: true, message: '请选日期类型', trigger: 'change' }],
          itemList: [
            { text: '送修日期', value: '1' },
            { text: '结算日期', value: '2' },
            { text: '项目派工日期', value: '3' },
            { text: '项目完工日期', value: '4' },
            { text: '审核日期', value: '5' },
            { text: '出厂日期', value: '6' }
            // { text: '出厂日期', value: '6' }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '日期范围',
          style: { minWidth: '200px' },
          fieldName: 'startTime|endTime',
          initialValue: [
            moment(new Date())
              .subtract(1, 'month')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD')
          ]
          // rules: [{ required: true, message: '请选日期范围', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '委托书类别',
          fieldName: 'REPAIR_TYPE_CODE',
          placeholder: '请选择...',
          itemList: this.createDictList('9002')
        },
        {
          type: 'SELECT',
          label: '类别',
          fieldName: 'PART_OR_LABOUR',
          initialValue: '1',
          placeholder: '请选择...',
          rules: [{ required: true, message: '请选类型', trigger: 'change' }],
          itemList: [
            { text: '全部', value: '1' },
            { text: '项目', value: '2' },
            { text: '材料', value: '3' }
          ]
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'MODEL_NAME',
          placeholder: '请选择...',
          unitRender: () => {
            return <el-button slot="append" icon="el-icon-search" onClick={this.openManager7}></el-button>;
          }
        },
        {
          type: 'SELECT',
          label: '主修人',
          fieldName: 'CHIEF_TECHNICIAN',
          initialValue: '',
          itemList: []
        },
        // {
        //   type: 'CHECKBOX',
        //   label: '建议维修项目',
        //   fieldName: 'NEEDLESS_REPAIR',
        //   options: {
        //     trueValue: '10041001',
        //     falseValue: '10141002'
        //   }
        // },
        {
          type: 'INPUT',
          label: '牌照号',
          fieldName: 'LICENSE'
        },
        {
          type: 'INPUT',
          label: '项目/备件名称',
          fieldName: 'LABOUR_PART_NAME'
        },
        {
          type: 'SELECT',
          label: '服务顾问',
          fieldName: 'SERVICE_ADVISOR',
          placeholder: '请选择...',
          itemList: []
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '委托书编号',
          dataIndex: 'RO_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '工时',
          dataIndex: 'STD_LABOUR_HOUR',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '项目/备件名称',
          dataIndex: 'LABOUR_PART_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '主修人/领料人',
          dataIndex: 'CHIEF_TECHNICIAN',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          filterItems: [],
          dictItems: []
        },
        {
          title: '工时费',
          dataIndex: 'LABOUR_AMOUNT',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '底盘号',
          dataIndex: 'VIN',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          editType: 'select',
          editItems: [],
          filterItems: [],
          dictItems: []
        },
        {
          title: '服务顾问代码',
          dataIndex: 'SERVICE_ADVISOR_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '牌照号',
          dataIndex: 'LICENSE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '维修状态',
          dataIndex: 'RO_STATUS',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
          // filterItems: this.createDictList('8049'),
          // render: props => {
          //   return <span>{this.createDictText(props.row.RO_STATUS, '8049')}</span>;
          // }
        },

        {
          title: '工单备注',
          dataIndex: 'REMARK',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },

        {
          title: '结算日期',
          dataIndex: 'BALANCE_TIME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd',
          filterType: 'date-range',
          editable: false,
          editType: 'date-picker',
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '项目/备件代码',
          dataIndex: 'LABOUR_PART_NO',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '合计数量',
          dataIndex: 'QUANTITY',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '合计金额',
          dataIndex: 'AMOUNT',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '维修类型',
          dataIndex: 'REPAIR_TYPE_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '品牌',
          dataIndex: 'BRAND',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '类别',
          dataIndex: 'category',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '优惠后工时费',
          dataIndex: 'discountRealAmount',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '优惠后材料费',
          dataIndex: 'discountRealPartAmount',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '班组',
          dataIndex: 'WORKGROUP_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '累计维修次数',
          dataIndex: 'HISTORY_REPAIR_TIMES',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '行驶里程',
          dataIndex: 'CURRENT_MILEAGE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input',
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '车型名称',
          dataIndex: 'MODEL_NAME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '预交车时间',
          dataIndex: 'END_TIME_SUPPOSED',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '检验人',
          dataIndex: 'CHECKER',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'checkbox',
          filterItems: [],
          dictItems: []
        },
        {
          title: '派工时间',
          dataIndex: 'ASSIGN_TIME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '维修开始时间',
          dataIndex: 'ITEM_START_TIME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '维修结束时间',
          dataIndex: 'ITEM_END_TIME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          dateFormat: 'yyyy-MM-dd HH:mm:ss',
          filterType: 'date-range',
          exportFormat: {
            t: 'd'
          }
        },
        {
          title: '完工标识',
          dataIndex: 'FINISHED_TAG',
          sorter: true,
          filter: true,
          filterType: 'radio',
          filterItems: [
            { text: '是', value: '10041001' },
            { text: '否', value: '10041002' }
          ],
          editable: true,
          defaultEditable: true,
          disabled: true,
          editType: 'checkbox',
          editItems: [
            { text: '', trueValue: '10041001' },
            { text: '', falseValue: '10041002' }
          ],
          dictItems: this.createDictList(1004)
        },

        {
          title: '追加项目',
          dataIndex: 'ADD_TAG',
          sorter: true,
          filter: true,
          filterType: 'radio',
          filterItems: [
            { text: '是', value: '是' },
            { text: '否', value: '否' }
          ],
          editable: true,
          defaultEditable: true,
          disabled: true,
          editType: 'checkbox',
          editItems: [
            { text: '', trueValue: '是' },
            { text: '', falseValue: '否' }
          ]
        },
        {
          title: '服务包',
          dataIndex: 'PACKAGE',
          sorter: true,
          filter: true,
          filterType: 'radio',
          filterItems: [
            { text: '是', value: '是' },
            { text: '否', value: '否' }
          ],
          editable: true,
          defaultEditable: true,
          disabled: true,
          editType: 'checkbox',
          editItems: [
            { text: '', trueValue: '是' },
            { text: '', falseValue: '否' }
          ]
        },
        {
          title: '服务包代码',
          dataIndex: 'PACKAGE_CODE',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        },
        {
          title: '有效时间',
          dataIndex: 'EFFECTIVE_TIME',
          sorter: true,
          filter: true,
          showOverflowTooltip: true,
          filterType: 'input'
        }
      ];
    },
    entering() {
      if (this.tableData.length > 0) {
        this.selectData = this.tableData[0];
        this.labour = true;
      } else {
        this.$notify.warning({ title: '提示信息', message: '请选择一条工单信息！' });
      }
    },
    selectDataList(val) {
      this.tableData = val;
      this.printList = val;
    },
    openManager7() {
      this.visible7 = true;
    },
    openManager8() {
      this.visible8 = true;
    },
    confirmDialogManager7(val) {
      this.topFilterList.forEach(x => {
        if (x.fieldName === 'MODEL_NAME') {
          console.log('车型', val);
          x.initialValue = val[0].MODEL_NAME;
          this.modelCode = val[0].MODEL_CODE;
        }
      });
      this.cancelDialogManager7();
      this.topFilterList = [...this.topFilterList];
    },

    confirmDialogManager8(val) {
      this.topFilterList.forEach(x => {
        if (x.fieldName === 'MODEL_CODE') {
          x.initialValue = val[0].MODEL_CODE;
        }
      });
      this.cancelDialogManager8();
      this.topFilterList = [...this.topFilterList];
    },
    cancelDialogManager7() {
      this.visible7 = false;
    },
    cancelDialogManager8() {
      this.visible8 = false;
    },
    saveHandle() {
      this.$refs.print.EXCUTE_PRINT();
    },
    // Portal
    Portal() {
      this.parmas = this.tableData[0].VIN;
      this.PortalShow = true;
    },
    closeDialog() {
      this.labour = false;
    },

    closePortalHandler(val) {
      // 关闭页面
      this.PortalShow = val;
      this.fdparam = val;
      this.ElsaPro = val;
      this.localthink = val;
    },
    resetChange() {
      // 重置
      this.modelCode = null;
    },
    changeHandle(val) {
      // if (val.startTime || val.endTime) {
      //   let diffMonth = moment(val.endTime).diff(moment(val.startTime), 'months');
      //   if (diffMonth > 2) return this.$notify.warning({ title: '提示信息', message: '查询日期跨度不能超过三个月' });
      // }
      this.fetchParams = Object.assign({}, val, { modelCode: this.modelCode });
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table && this.$refs.table.EXECUTE_RESET_HEIGHT();
      });
    },
    async saveDialog() {
      let res = {};
      if (this.tableData == null) return;
      res = await addCustomeRgradeUpdatet({ customeRgradeUpdateDTOList: this.tableData });
      if (res.resultCode === 200) {
        this.closeDialog('refresh');
      }
    },
    showHandler() {
      // 显示
      this.dialogTitle = `查看委托书明细`;
      this.visible = true;
    },
    closeHandler(val, type) {
      // 关闭
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
};
</script>

<style lang="less" scoped></style>
